﻿@model Retete.ViewModels.VanzareViewModel
@{
    ViewBag.Title = "Vanzare";
}

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery-1.4.4.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery-ui.min.js")" type="text/javascript"></script>

<script type="text/javascript">
    window.onload = function () {
        DenumireVanzare.focus();
        setVisibility();
    }

    function submitOnEnter() {
        if (event.keyCode == "13") form.submit();
    }
    function refreshOnEnter() {
        if (event.keyCode == "13") {
            Refresh.value = true;
            form.submit();
        }
    }
    function stergeInregistrare(id) {
        if (confirm('Doriti sa stergeti produsul selectat?') == true) {
            IDElementSters.value = id;
            form.submit();
        }
    }
    function inregistreazaVanzare() {
        InregistreazaVanzare.value = true;
        form.submit();
    }
    function setVisibility() {
        if (TiparesteFactura.checked == true) pnlDateFactura.setAttribute("style", "display: block");
        else pnlDateFactura.setAttribute("style", "display: none");
    }
    function tipVanzareChanged() {
        if (TipVanzare.value == "Plata numerar") EmiteBon.checked = true;
        else if (TipVanzare.value == "Plata prin virament") EmiteBon.checked = true;
        else EmiteBon.checked = false;
    }
</script>

<script type="text/javascript">
    $(function () {
        $("#DenumireVanzare").autocomplete({
            source: function (request, response) {
                $.ajax({
                    url: '@Url.Content("~/Autocomplete/ElementeVanzare")',
                    dataType: "json",
                    data: { maxRows: 8, contains: request.term },
                    success: function (data) {
                        response($.map(data, function (item) {
                            return { value: item.Cod + ' ' + item.Nume, idReteta: item.IDReteta, idMeniu: item.IDMeniu, idProdus: item.IDProdus, pret: item.Pret, cod: item.Cod, nume: item.Nume }
                        }));
                    }
                });
            },
            minLength: 2,
            delay: 50,
            select: function (event, ui) {
                if (ui.item.idMeniu != -1) IDMeniuVandut.value = ui.item.idMeniu;
                else if (ui.item.idReteta != -1) IDRetetaVanduta.value = ui.item.idReteta;
                else if (ui.item.idProdus != -1) IDProdusVandut.value = ui.item.idProdus;

                DenumireVanzare.value = ui.item.nume;
                PretVanzare.value = ui.item.pret;
                ElementVanzare_Cod.innerText = ui.item.cod;
            }
        });
    });
    $(function () {
        $("#ClientNume").autocomplete({
            source: function (request, response) {
                $.ajax({
                    url: '@Url.Content("~/Autocomplete/Clienti")',
                    dataType: "json",
                    data: { maxRows: 8, contains: request.term },
                    success: function (data) {
                        response($.map(data, function (item) {
                            return { value: item.ClientNume, nume: item.ClientNume, adresa: item.ClientAdresa, banca: item.ClientBanca, codFiscal: item.ClientCodFiscal, codRegistruComert: item.ClientCodRegistruComert, cont: item.ClientCont, judet: item.ClientJudet }
                        }));
                    }
                });
            },
            minLength: 2,
            delay: 50,
            select: function (event, ui) {
                ClientNume.value = ui.item.nume;
                ClientAdresa.value = ui.item.adresa;
                ClientJudet.value = ui.item.judet;
                ClientCodFiscal.value = ui.item.codFiscal;
                ClientCodRegistruComert.value = ui.item.codRegistruComert;
                ClientCont.value = ui.item.cont;
                ClientBanca.value = ui.item.banca;
            }
        });
    });

    $(function () {
        $("#Data").datepicker({ dateFormat: 'mm/dd/yy' });
    });
</script>

@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "form", name = "form" }))
{
    <table width="100%">
        <tr>
            <td style="width: 190px; vertical-align: top; border-right-style: dashed; border-right-color: #CFCFCF; border-right-width: 1px;">
                <fieldset>
                    <legend>Activitati</legend>

                    <p class="toolbox" style="font-size: 18px"><a href="javascript:inregistreazaVanzare()">Inregistreaza vanzare</a></p>
                    <p class="toolbox" style="font-size: 18px">@Html.ActionLink("Resteaza vanzare", "Create")</p>
                </fieldset>
            </td>
            <td valign="top">
                <fieldset>
                    @Html.ValidationSummary(false)
                    @Html.HiddenFor(model => model.IDElementSters)
                    @Html.HiddenFor(model => model.GenereazaBon)
                    @Html.HiddenFor(model => model.Refresh)
                    @Html.HiddenFor(model => model.InregistreazaVanzare)

                    @for (int i = 0; i < Model.TipuriVanzare.Count; i++)
                    {
                        @Html.HiddenFor(model => model.TipuriVanzare[i])
                    }

                    <legend>Configurare vanzare</legend>

                    <p>
                        @Html.LabelFor(model => model.TipVanzare, "Tip vanzare")
                        @Html.DropDownListFor(model => model.TipVanzare, new SelectList(Model.TipuriVanzare), new { style = "width: 300px;", onchange = "tipVanzareChanged()" })
                        @Html.CheckBoxFor(model => model.TiparesteFactura, new { style = "width: 20px;", onclick = "setVisibility()" })
                        Tipareste factura
                    </p>

                    <p>
                        @Html.LabelFor(model => model.Data, "Data")
                        @Html.TextBoxFor(model => model.Data, new { style = "width: 294px;" })
                        @Html.CheckBoxFor(model => model.EmiteBon, new { style = "width: 20px;" })
                        Emite bon
                    </p>

                    <p>
                        @Html.LabelFor(model => model.ClientNume, "Client")
                        @Html.TextBoxFor(model => model.ClientNume, new { style = "width: 294px;" })
                    </p>

                    <div id="pnlDateFactura" style="display: none">
                        <p>
                            @Html.LabelFor(model => model.FacturaSerie, "Serie si nr factura")
                            @Html.TextBoxFor(model => model.FacturaSerie, new { style = "width: 142px" })
                            @Html.TextBoxFor(model => model.FacturaNumar, new { style = "width: 143px" })
                            @Html.CheckBoxFor(model => model.TiparesteProdusele, new { style = "width: 20px;" })
                            Tipareste produsele
                        </p>

                        <p>
                            @Html.LabelFor(model => model.ClientAdresa, "Adresa si judet")
                            @Html.TextBoxFor(model => model.ClientAdresa, new { style = "width: 192px" })
                            @Html.TextBoxFor(model => model.ClientJudet, new { style = "width: 93px" })
                        </p>

                        <p>
                            @Html.LabelFor(model => model.ClientCodFiscal, "CUI si cod reg. com")
                            @Html.TextBoxFor(model => model.ClientCodFiscal, new { style = "width: 142px" })
                            @Html.TextBoxFor(model => model.ClientCodRegistruComert, new { style = "width: 143px" })
                        </p>

                        <p>
                            @Html.LabelFor(model => model.ClientCont, "Cont si banca")
                            @Html.TextBoxFor(model => model.ClientCont, new { style = "width: 142px" })
                            @Html.TextBoxFor(model => model.ClientBanca, new { style = "width: 143px" })
                        </p>
                    </div>
                </fieldset>
            </td>
            <td valign="top" style="width: 300px">
                <fieldset>
                    <legend>Bonuri de masa</legend>

                    <table class="grid" width="100%">
                        <thead>
                            <tr>
                                <th>Valoare bon</th>
                                <th>Numar</th>
                                <th>Valoare</th>
                            </tr>
                        </thead>

                        <tbody>
                            <tr class="insertionRow">
                                <td>@Html.TextBoxFor(element => element.ValoareBonNou, new { autocomplete = "off" })</td>
                                <td>@Html.TextBoxFor(element => element.NumarBonuriNoi, new { autocomplete = "off", onkeydown = "return submitOnEnter()" })</td>
                                <td></td>
                            </tr>

                            @if (Model.Bonuri != null)
                            {
                                for (int i = 0; i < Model.Bonuri.Count; i++)
                                {
                                    <tr>
                                        <td>
                                            @Model.Bonuri[i].ValoareBon.ToString("0.00")
                                            @Html.HiddenFor(element => element.Bonuri[i].ValoareBon)
                                        </td>
                                        <td class="insertionCell">@Html.TextBoxFor(element => element.Bonuri[i].Numar, new { autocomplete = "off", onkeydown = "return refreshOnEnter()" })</td>
                                        <td>@Model.Bonuri[i].Valoare.ToString("0.00", System.Globalization.CultureInfo.InvariantCulture)</td>
                                    </tr>
                                }
                            }
                            
                            <tr class="totals">
                                <td>Total:</td>
                                <td></td>
                                <td>
                                    @if (@Model.Bonuri != null)
                                    { 
                                        @Model.Bonuri.Sum(element => element.Valoare).ToString("0.00") 
                                    }
                                    else
                                    {
                                        <span>0.00</span>
                                    }
                                </td>
                            </tr>
                        </tbody>
                    </table>            
                </fieldset>
            </td>
        </tr>
        <tr>
            <td></td>
            <td colspan="2">
                <fieldset>
                    <legend>Produse vandute</legend>
            
                    <table class="grid">
                        <thead>
                            <tr>
                                <th style="width: 16px"></th>
                                <th style="width: 70px">Cod</th>
                                <th>Meniu sau reteta</th>
                                <th style="width: 110px">Pret</th>
                                <th style="width: 110px">Portii</th>
                            </tr>
                        </thead>
        
                        <tbody id="tableBody">
                            <tr class="insertionRow">
                                <td></td>
                                <td id="ElementVanzare_Cod"></td>
                                <td>
                                    @Html.TextBoxFor(model => model.DenumireVanzare, new { autocomplete = "off" })
                                    @Html.HiddenFor(model => model.IDMeniuVandut)
                                    @Html.HiddenFor(model => model.IDRetetaVanduta)
                                    @Html.HiddenFor(model => model.IDProdusVandut)
                                </td>
                                <td>@Html.TextBoxFor(model => model.PretVanzare, new { autocomplete = "off" })</td>
                                <td>@Html.TextBoxFor(model => model.PortiiVandute, new { autocomplete = "off", onkeydown = "return submitOnEnter()" })</td>
                            </tr>

                            @if (Model.ElementeVanzare != null)
                            {
                                for (int i = 0; i < Model.ElementeVanzare.Count; i++)
                                {
                                    <tr>
                                        <td>
                                            <img src="@Url.Content("~/Content/Images/Delete.png")" alt="Sterge" style="height: 12px; cursor: pointer;" onclick="stergeInregistrare(@Model.ElementeVanzare[i].ID)" />
                                            @Html.HiddenFor(element => element.ElementeVanzare[i].ID)
                                        </td>
                                        <td>
                                            @Html.DisplayTextFor(element => element.ElementeVanzare[i].Cod)
                                            @Html.HiddenFor(element => element.ElementeVanzare[i].Cod)
                                        </td>
                                        <td>
                                            @Html.DisplayTextFor(element => element.ElementeVanzare[i].Nume)
                                            @Html.HiddenFor(element => element.ElementeVanzare[i].Nume)
                                            @Html.HiddenFor(element => element.ElementeVanzare[i].IDReteta)
                                            @Html.HiddenFor(element => element.ElementeVanzare[i].IDMeniu)
                                            @Html.HiddenFor(element => element.ElementeVanzare[i].IDProdus)
                                        </td>
                                        <td>
                                            @Model.ElementeVanzare[i].Pret.ToString("0.00")
                                            @Html.HiddenFor(element => element.ElementeVanzare[i].Pret)
                                        </td>
                                        <td>
                                            @Html.DisplayTextFor(element => element.ElementeVanzare[i].Portii)
                                            @Html.HiddenFor(element => element.ElementeVanzare[i].Portii)
                                        </td>
                                    </tr>
                                }
                            }
                            
                            <tr class="totals">
                                <td></td>
                                <td></td>
                                <td>Total:</td>
                                <td style="font-size: 22px; font-weight: bold;">@Model.Valoare.ToString("0.00")</td>
                                <td></td>
                            </tr>
                            <tr class="totals">
                                <td></td>
                                <td></td>
                                <td>Total cu bonuri de masa:</td>
                                <td style="font-size: 22px; font-weight: bold;">@Model.RestPlata.ToString("0.00")</td>
                                <td></td>
                            </tr>
                        </tbody>
                    </table>
                </fieldset>
            </td>
        </tr>
    </table>
}